//
//  ItemSecondViewController.m
//  Lovelife
//
//  Created by apple on 15/4/27.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ItemSecondViewController.h"
#import "AFNetworking.h"
#import "ItemSecondModel.h"
#import "ItemSecondCell.h"
#import "UIImageView+WebCache.h"
#import "ItemSecondNextViewController.h"
#import "MJRefresh.h"
#import "NightManager.h"

@interface ItemSecondViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,copy)NSString *strURL;

@property (nonatomic,retain)NSMutableArray *modelArr;

@property (nonatomic,retain)UITableView *tableView;

@property (nonatomic,retain)UIActivityIndicatorView *acIntor;

@property (nonatomic,assign)NSInteger number;

@property (nonatomic,assign)BOOL temp;

@end

@implementation ItemSecondViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        
        self.modelArr = [NSMutableArray array];
        
        self.temp = YES;
        
        self.number = 1;
    }
    return self;


}
- (void)dealloc
{
    [_modelArr release];
    
    [_tableView release];
    
    [_acIntor release];
    
    [super dealloc];
}

-(void)getData
{
    if (self.temp == YES) {
        [self.acIntor startAnimating];
    }
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    self.strURL = [NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.topic.list.php?p=%ld&pagesize=%d&order=addtime",self.number,arc4random()%(20)+1];
    [manager GET:self.strURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
//        ItemSecondModel *second = [[ItemSecondModel alloc]init];
//        
//        [second setValuesForKeysWithDictionary:responseObject[@"pagesize"]];
//        
//        [second setValuesForKeysWithDictionary:responseObject[@"nowpage"]];
//        
//        [self.modelArr addObject:second];
//        
//        [second release];
        
        NSMutableArray *tempArr = responseObject[@"results"];
        
        for (NSMutableDictionary *dic in tempArr) {
            
            ItemSecondModel *second = [[ItemSecondModel alloc]init];
            
            [second setValuesForKeysWithDictionary:dic];
            
            [self.modelArr addObject:second];
            
            [second release];
        }
        if (self.temp == YES) {
            [self.acIntor stopAnimating];
            [self createView];
        }
        [self.tableView reloadData];

        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"错误信息%@",error);
    }];


}

-(void)createAcIntor
{
    self.acIntor = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [self.acIntor setCenter:self.view.center];
    [self.acIntor setBackgroundColor:[UIColor grayColor]];
    [self.acIntor setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:self.acIntor];
    [_acIntor release];
    
}
//创建tableview
-(void)createView
{

    self.tableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStyleGrouped];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.tableView];
    
    [_tableView release];
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    [self.tableView addHeaderWithCallback:^{
       
        [self.modelArr removeAllObjects];
        
        [self.tableView reloadData];
        
        [self getData];
        
        [self.tableView headerEndRefreshing];
        
    }];
    
    [self.tableView addFooterWithCallback:^{
       
        self.number ++;
        self.temp = NO;
        [self getData];
        [self.tableView footerEndRefreshing];
        
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self createAcIntor];
    [self getData];
    
    
}

-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.modelArr.count ;

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    
    ItemSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    
    if (!cell) {
        cell = [[[ItemSecondCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
    }
    ItemSecondModel *sec = self.modelArr[indexPath.row];
    cell.title.text = sec.title;

    
    [cell.Img sd_setImageWithURL:[NSURL URLWithString:sec.thumb]];
    
    return cell;
    
    


}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height/6+100;


}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ItemSecondNextViewController *next = [[ItemSecondNextViewController alloc]init];
    
    ItemSecondModel *model = self.modelArr[indexPath.row];
    
    next.ID = model.ID;

    next.aboveDataModel = self.modelArr;
    
    [self.navigationController pushViewController:next animated:YES];
    
    [next release];


}
-(void)setNightDayModel
{
    [NightManager setBackgroundColorWithView:self.view];
    
}

-(void)setLightDayModel
{
    [NightManager setBackgroundColorWithView:self.view];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
